/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tixian.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.achieve.entity.Achievement;
import com.jeeplus.modules.achieve.service.AchievementService;
import com.jeeplus.modules.cashcharge.entity.CashCharge;
import com.jeeplus.modules.cashcharge.service.CashChargeService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.purchaseorder.entity.PurchaseOrder;
import com.jeeplus.modules.purchaseorder.service.PurchaseOrderService;
import com.jeeplus.modules.userbank.entity.Userbank;
import com.jeeplus.modules.userbank.service.UserbankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.tixian.entity.Tixian;
import com.jeeplus.modules.tixian.mapper.TixianMapper;

/**
 * 用户提现记录Service
 * @author freedom
 * @version 2020-08-07
 */
@Service
@Transactional(readOnly = true)
public class TixianService extends CrudService<TixianMapper, Tixian> {

	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private MemberService memberService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private CashChargeService cashChargeService;
	@Autowired
	private PurchaseOrderService purchaseOrderService;
	@Autowired
	private UserbankService userbankService;

	public Tixian get(String id) {
		return super.get(id);
	}
	
	public List<Tixian> findList(Tixian tixian) {
		return super.findList(tixian);
	}
	
	public Page<Tixian> findPage(Page<Tixian> page, Tixian tixian) {
		String managerId = "";
		List<Tixian> accountList = new ArrayList<>();
		dataRuleFilter(tixian);
		tixian.setPage(page);
		if (tixian.getManager() != null){
			managerId = tixian.getManager().getId();
		}
		if (StringUtils.isNotBlank(managerId)){

			Member member1 = new Member();
			member1.setP(tixian.getManager());
//			member.setP(member.getManager());
			List<Member> memberList = memberService.findList(member1);
			//查询团队下级人员
			List<Member> queryList = new ArrayList<>();
			//最终集合
			List<Member> list = new ArrayList<>();
			queryList.addAll(memberList);
			list.addAll(memberList);
			while (list.size() > 0) {
				list.clear();
				List<Member> list1 = new ArrayList<>();
				for (Member member2 : queryList) {
					Member entity = new Member();
					entity.setP(member2);
//					member.setP(member2);
					List<Member> memberList1 = memberService.findList(entity);
					memberList.addAll(memberList1);
					list1.addAll(memberList1);
				}
				list.addAll(list1);
				queryList.clear();
				queryList.addAll(list1);
			}
			if (memberList.size() >0){
				for (Member member : memberList) {
					Tixian tixian1 = new Tixian();
					tixian1.setMember(member);
					List<Tixian> list1 = findList(tixian1);
					accountList.addAll(list1);
				}
			}
			page.setCount(accountList.size());
			page.setList(accountList);
		}else {
			List<Tixian
					> list = findList(tixian);
			page.setList(list);
		}

		return page;
//		return super.findPage(page, tixian);
	}
	
	@Transactional(readOnly = false)
	public void save(Tixian tixian) {
		//审核失败   提现金额返还钱包余额
//		if ("2".equals(tixian.getShenhe())){
//			Account account = new Account();
//			account.setMember(tixian.getMember());
//			List<Account> accountList = accountMapper.findList(account);
//			if (accountList.size()>0){
//				BigDecimal money = BigDecimal.valueOf(accountList.get(0).getYue()).add(BigDecimal.valueOf(tixian.getMoney()));
//				accountList.get(0).setYue(money.doubleValue());
//				accountList.get(0).preUpdate();
//				accountMapper.update(accountList.get(0));
//			}
//		}
		super.save(tixian);
	}
	
	@Transactional(readOnly = false)
	public void delete(Tixian tixian) {
		super.delete(tixian);
	}

	public List<Tixian> findTodayList(Tixian tixian) {
		return mapper.findTodayList(tixian);
	}

	@Transactional(readOnly = false)
	public void confirmCash(Tixian tixian) {
		tixian.setShenhe("1");
		super.save(tixian);

	}

	/**
	 * 确认提现
	 * @param member
	 * @param cashMoney
	 * @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson cashOut(Member member, String cashMoney) {
		AjaxJson aj = new AjaxJson();
		List<CashCharge> charges = cashChargeService.findAllList(new CashCharge());
//			aj.setSuccess(true);
//			aj.setMsg("提现功能暂时关闭。");
		//提现金额
		BigDecimal cash = BigDecimal.valueOf(Double.valueOf(cashMoney));
		if (cash.compareTo(BigDecimal.valueOf(charges.get(0) != null?charges.get(0).getStartMoney():0)) == -1
				|| (cash.compareTo(BigDecimal.valueOf(charges.get(0) != null?charges.get(0).getStartMoney():0)) == 1
				&& !(cash.doubleValue() % (charges.get(0) != null?charges.get(0).getMagnification():0) == 0))){
			aj.setSuccess(false);
			aj.setMsg("请输入正确金额,最低"+charges.get(0).getStartMoney()+"且"+charges.get(0).getMagnification()+"的倍数。");
			return aj;
		}
		//查询钱包余额
		Account account = new Account();
		account.setMember(member);
		List<Account> accountList = accountService.findList(account);
		if (accountList.size()<=0){
			aj.setSuccess(false);
			aj.setMsg("用户钱包信息有误");
			return aj;
		}
		//查询用户现在的待交易的买入订单
		PurchaseOrder purchaseOrder = new PurchaseOrder();
		purchaseOrder.setMember(member);
		purchaseOrder.setIsRevoke("0");
		purchaseOrder.setIsDeal("0");
		List<PurchaseOrder> list = purchaseOrderService.findList(purchaseOrder);
		double totalMoney = 0.0;
		if (list.size()>0){
			totalMoney = list.stream().mapToDouble(PurchaseOrder::getTotalPrice).sum();
		}
		BigDecimal balance = BigDecimal.valueOf(accountList.get(0).getYue()).subtract(BigDecimal.valueOf(totalMoney));
		if (balance.compareTo(cash) == -1){
			aj.setSuccess(false);
			aj.setMsg("钱包余额不足");
			return aj;
		}
		//用户银行卡信息
		Userbank userbank = new Userbank();
		userbank.setMember(member);
		List<Userbank> userbankList = userbankService.findList(userbank);
		if (userbankList.size()<=0){
			aj.setSuccess(false);
			aj.setMsg("用户银行卡信息有误，请确认");
			return aj;
		}

		BigDecimal charge = BigDecimal.valueOf(charges.get(0) != null?charges.get(0).getServiceCharge():0)
				.divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_HALF_DOWN);
		accountList.get(0).setYue(BigDecimal.valueOf(accountList.get(0).getYue()).subtract(cash).doubleValue());
//			if ("0".equals(flag)){
//				if (cash.compareTo(BigDecimal.valueOf(10000)) == 1){
//					charge = cash.multiply(charge).add(BigDecimal.valueOf(20));
//				}else {
		charge = cash.multiply(charge).add(BigDecimal.valueOf(10));
//				}
//			}
		accountService.save(accountList.get(0));

		//插入提现记录
		Tixian tixian = new Tixian();
		tixian.setMoney(cash.doubleValue());
		tixian.setArrivalMoney(cash.subtract(charge).doubleValue());
		tixian.setBank(userbankList.get(0).getBankname());
		tixian.setBankno(userbankList.get(0).getBankno());
		tixian.setMember(member);
		tixian.setShenhe("0");
		tixian.setName(userbankList.get(0).getName());
		super.save(tixian);

		aj.setSuccess(true);
		aj.setMsg("操作成功");
		return aj;
	}
}